﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;

namespace Framework.Common.Cache.ICacheImplement
{
    /// <summary>
    /// cache in memory
    /// </summary>
    public class InMemoryCache : ICache
    {
        #region private member

        private readonly MemoryCache mc;

        #endregion

        #region constructor

        /// <summary>
        /// 
        /// </summary>
        public InMemoryCache()
        {
            mc = MemoryCache.Default;
        }

        #endregion

        #region ICache

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Add<T>(string key, T value)
        {
            return mc.Add(key, value, new DateTimeOffset(DateTime.MaxValue));
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expiredTime"></param>
        /// <returns></returns>
        public bool Add<T>(string key, T value, DateTime expiredTime)
        {
            return mc.Add(key, value, new DateTimeOffset(expiredTime));
        }

        /// <summary>
        /// 是否包含key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            return mc.Contains(key);
        }

        /// <summary>
        /// 获取缓存数
        /// </summary>
        public int Count
        {
            get { return mc.Count(); }
        }

        /// <summary>
        /// 清空缓存
        /// </summary>
        public void Flush()
        {
            mc.Trim(100);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            return mc.Get(key);
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            object obj = Get(key);
            if (obj != null)
            {
                return (T)obj;
            }
            return default(T);
        }

        /// <summary>
        /// 删除key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            mc.Remove(key);
            return true;
        }

        #endregion


        public bool Set<T>(string key, T value)
        {
            mc.Set(key, value, null);
            return true;
        }

        public bool Set<T>(string key, T value, DateTime expiredTime)
        {
            mc.Set(key, value, new DateTimeOffset(expiredTime));
            return true;
        }

        public bool MultiSet(IDictionary<string, object> dict)
        {
            dict.Each(kv => mc.Set(kv.Key, kv.Value, null));
            return true;
        }

        public bool MultiSet<T>(IDictionary<string, T> dict)
        {
            dict.Each(kv => mc.Set(kv.Key, kv.Value, null));
            return true;
        }

        public IDictionary<string, object> MultiGet(IEnumerable<string> keys)
        {
            return keys.Select(key => new { Key = key, val = Get(key) }).ToDictionarySafe(i => i.Key, i => i.val);
        }

        public IDictionary<string, T> MultiGet<T>(IEnumerable<string> keys)
        {
            return keys.Select(key => new { Key = key, val = Get<T>(key) }).ToDictionarySafe(i => i.Key, i => i.val);
        }
    }
}